package com.syyo.gulimall.seckill.controller;

import com.syyo.common.domain.ResultVo;
import com.syyo.common.utils.ResultUtils;
import com.syyo.gulimall.seckill.service.SeckillService;
import com.syyo.gulimall.seckill.to.SeckillSkuRedisTo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: wang
 * @date: 2021/5/21 11:46
 * @Description:
 */
@RestController
public class SeckillController {

    @Autowired
    SeckillService seckillService;

    @GetMapping("/currentSeckillSkus")
    public ResultVo getCurrentSeckillSkus(){
        System.out.println("=========currentSeckillSkus=========");
        List<SeckillSkuRedisTo> list = seckillService.getCurrentSeckillSkus();
        return ResultUtils.ok(list);
    }

    @GetMapping("/sku/seckill/{skuId}")
    public ResultVo getSkuSeckillInfo(@PathVariable("skuId")Long skuId){
        SeckillSkuRedisTo to = seckillService.getSkuSeckillInfo(skuId);
        return ResultUtils.ok(to);
    }

    /**
     * 商品进行秒杀(秒杀开始)
     * @param killId
     * @param key
     * @param num
     * @return
     */
    @GetMapping(value = "/kill")
    public String seckill(@RequestParam("killId") String killId,
                          @RequestParam("key") String key,
                          @RequestParam("num") Integer num,
                          Model model) {

        String orderSn = null;
        try {
            //1、判断是否登录
            orderSn = seckillService.kill(killId,key,num);
            model.addAttribute("orderSn",orderSn);
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println("=========="+orderSn+"=============");
        return orderSn;
    }

}
